package threecard.android;

import threecard.android.httpHelper.AccountDbHelper;
import threecard.android.room.ViewRoom;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class Login extends MainActivity implements OnClickListener {
	SharedPreferences mGameSettings;
	public ProgressDialog progressDialog;
	private EditText userEditText;
	private EditText passEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// Progress bar.
		
		progressDialog = new ProgressDialog(this);
		// TODO: use a string in resource instead of this.
		progressDialog.setMessage("Please wait ...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		
		mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
		// initialize to EditText: userEditText and passEditText.
		userEditText = (EditText) findViewById(R.id.username);
		passEditText = (EditText) findViewById(R.id.password);

		View loginButton = findViewById(R.id.login_login_button);
		loginButton.setOnClickListener(this);

		View cancelButton = findViewById(R.id.login_cancel_button);
		cancelButton.setOnClickListener(this);
		
		View forgotButton = findViewById(R.id.forgot_pass_button);
		forgotButton.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.d(DEBUG_TAG, "SHARED PREFERENCES");
        Log.d(DEBUG_TAG, "Email is: " + mGameSettings.getString(GAME_PREFERENCES_EMAIL, "Not set"));
        Log.d(DEBUG_TAG, "Email is: " + mGameSettings.getString(GAME_PREFERENCES_USERNAME, "Not set"));
        Log.d(DEBUG_TAG, "Password is: " + mGameSettings.getString(GAME_PREFERENCES_PASSWORD, "Not set"));
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_login_button:
			int usersize = userEditText.getText().length();
			int passsize = passEditText.getText().length();

			// First check the validity of the username and password.
			if (usersize > 0 && passsize > 0) {
				//progressDialog.show(); // Show progress dialog.
				String user = userEditText.getText().toString(); // get username.
				String pass = md5(passEditText.getText().toString()); // get password.
				Log.i("ERROR  LOGIN","username and password: "+user+", "+pass);
				AccountDbHelper account = new AccountDbHelper( user, pass);
				String result = account.actionLogin();
				if (result != "FAILED"){
					//get shared preference and save it
					Log.i("CHECK TOKEN",result);
					Editor editor = mGameSettings.edit();
                    editor.putString(GAME_PREFERENCES_USERNAME, user);
                    editor.putString(GAME_PREFERENCES_PASSWORD, pass);
                    editor.putString(GAME_PREFERENCES_TOKEN, result);
                    editor.commit();
					Intent hello_screen_intent = new Intent(this, ViewRoom.class);
					startActivity(hello_screen_intent);
				}
				else {
					 createDialog("Fail","Sorry!! Incorrect Username or Password"); 
				}// process username and password.
			} else {
				// The user did not enter either username or password.
				// Display an error message.
				createDialog("Error", "Please enter Username and Password");
			}
			break;
		case R.id.forgot_pass_button:
			Intent forgot_intent = new Intent(this, Forgot.class);
			startActivity(forgot_intent);
			break;
			
		case R.id.login_cancel_button:
			setResult(RESULT_CANCELED);
			finish(); 
		
		default:
			break;
		}

	}
}
